package com.tujia.apollo;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.stereotype.Component;

import java.util.Set;
//@Component
public class ApolloAnnotationBean {
    //@ApolloConfig
    private Config defaultConfig; //注入默认namespace的config
    @ApolloConfig("1.application.public")
    private Config publicConfig; ////注入公共namespace(1.application.public)的config
    
    //默认namespace
    private Integer defaultCityId;
    //公共namespace
    private Integer publicCityId;

    //@ApolloConfigChangeListener
    private void someOnChange(ConfigChangeEvent changeEvent) {
        Set<String> changedKeys = changeEvent.changedKeys();
        for(String key : changedKeys){
            System.out.println("默认配置变化! oldValue = " +changeEvent.getChange(key).getOldValue());  
            System.out.println("默认配置变化! newValue = " +changeEvent.getChange(key).getNewValue());  
        }
    }

    //@ApolloConfigChangeListener("1.application.public")
    private void anotherOnChange(ConfigChangeEvent changeEvent) {
        Set<String> changedKeys = changeEvent.changedKeys();
        for(String key : changedKeys){
            System.out.println("公共配置变化! oldValue = " +changeEvent.getChange(key).getOldValue());
            System.out.println("公共配置变化! newValue = " +changeEvent.getChange(key).getNewValue());
        }
    }
    
    public Integer getDefaultCityId() {
        return this.defaultConfig.getIntProperty("apollo_cityID_property", 1);
    }
    
    public Integer getPublicCityId() {
        return publicConfig.getIntProperty("public_cityID", 1);
    }
}
